package com.bruce.springai.config;

import com.bruce.springai.config.advisor.LoggingAdvisor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatClientConfig {

    /**
     * 配置ChatClient
     * @param chatClientBuilder
     * @return
     */
    @Bean
    public ChatClient getChatClient(ChatClient.Builder chatClientBuilder, VectorStore vectorStore, ChatMemory chatMemory) {
        ChatClient chatClient = chatClientBuilder.defaultSystem("""
                        欢迎使用安达聊天机器人，请问你有什么需要帮忙的
                        """)
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory), // CHAT MEMORY
                        new QuestionAnswerAdvisor(vectorStore), // RAG
                        new LoggingAdvisor() // 添加 LoggingAdvisor 记录聊天对话的详细过程
                ).build();
        return chatClient;
    }

    /**
     * 配置ChatMemory 记录聊天对话的上下文
     * @return
     */
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }
}
